package com.cqut.forum.dao.imp;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.cqut.forum.dao.WallDao;
import com.cqut.forum.entity.FaceWall;

@Component
public class WallDaoImp extends BaseDaoImp implements WallDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getWallByOption(int index, int size,
			String option) {
		String sql = "select * from face_wall where 1=1 "+option+" order by publish_time desc";
		
		
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(option);
		
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, null, pages);
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> loadWall(String wallOwner,
			String wallName, String publishTime, int index, int size) {
		String sql = "select fw.wall_id,fw.picture_src,fw.wall_name,fw.publish_time,fw.wall_contents," +
				"u.user_name,fw.wall_state from face_wall fw,user u where fw.user_id=u.user_id ";
		if(!wallOwner.equals("")){
			sql = sql + "and u.user_name like \"%"+wallOwner+"%\" ";
		}
		if(!wallName.equals("")){
			sql = sql + "and fw.wall_name like \"%"+wallName+"%\" ";
		}
		if(null!=publishTime&&!"".equals(publishTime)){
			sql = sql + "and fw.publish_time>\""+publishTime+"\"";
		}
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, null, pages);
		return result;
	}

	@Override
	public boolean updateWallState(int wallId, boolean state) {
		String sql = "update face_wall set wall_state=? where wall_id=?";
		
		Query query = super.getSqlQuery(sql);
		query.setBoolean(0, state);
		query.setInteger(1, wallId);
		query.executeUpdate();
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public FaceWall getByPictureAddress(String pictureAddress) {
		String hql = "select fw from FaceWall fw where fw.pictureSrc=?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(pictureAddress);
		List<FaceWall> result = (List<FaceWall>) super.excuteHql(hql, parameters,null);
		
		return result.size()>0?result.get(0):null;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getWall(int userId,int index,int size) {
		String sql = "select fw.wall_id,fw.wall_contents,fw.wall_name,fw.picture_src,fw.publish_time from face_wall fw " +
				"where fw.user_id=? and fw.wall_state=true";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		
		Map<String, Integer> page = new HashMap<String, Integer>();
		page.put("index", index);
		page.put("size", size);
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters,page);
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countWall(int userId) {
		String sql = "select count(fw.wall_id) from face_wall fw where fw.user_id=? " +
			"and fw.wall_state=true";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		
		List<Map<String, BigInteger>> result = (List<Map<String, BigInteger>>) super.excuteSql(sql, parameters, null);
		return (Integer)(result.get(0).get("count(fw.wall_id)").intValue());
	}
	
}
